package com.sgb.member.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.sgb.member.R;
import com.sgb.member.databinding.ActivityMemberCompanyWebEditBinding;
import com.sgb.member.di.DaggerMemberComponent;
import com.sgb.member.di.MemberModule;
import com.sgb.member.entity.MemberBean;
import com.sgb.member.entity.MemberCompanyHeaderBean;
import com.sgb.member.view_model.MemberCompanyWebEditModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.util.MToast;
import com.swgk.core.util.StringUtil;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

/**
 * 会员企业网站-编辑
 */
public class MemberCompanyWebEditActivity extends AppActivity implements ItemInputTextView.OnWholeItemClickListener {

    @Inject
    MemberCompanyWebEditModel sModel;
    private ActivityMemberCompanyWebEditBinding mBinding;
    private MemberBean mMemberBean;
    private MemberCompanyHeaderBean mHeaderBean;

    public static void start(Activity context, MemberCompanyHeaderBean mHeaderBean, MemberBean memberBean, int code) {
        Intent intent = new Intent(context, MemberCompanyWebEditActivity.class);
        intent.putExtra("OBJECT", memberBean);
        intent.putExtra("OBJECT_HEADER", mHeaderBean);
        context.startActivityForResult(intent, code);
    }


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMemberComponent.builder()
                .appComponent(appComponent)
                .memberModule(new MemberModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        mMemberBean = (MemberBean) getIntent().getSerializableExtra("OBJECT");
        mHeaderBean = (MemberCompanyHeaderBean) getIntent().getSerializableExtra("OBJECT_HEADER");
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_member_company_web_edit);
        mBinding.include.titleView.setText("编辑企业信息");
        mBinding.include.ivBack.setOnClickListener(v -> onBackPressed());
        mBinding.btnRelease.setOnClickListener(this);
        mBinding.itemInputType.setOnWholeItemClickListener(this);
        // 会员类型隐藏 暂不修改会员类型
        mBinding.itemCompanyName.setContent(mMemberBean.getCompanyName());
        mBinding.etContacts.setContent(mMemberBean.getContacts());
        mBinding.etPhone.setContent(mMemberBean.getPhone());

        mBinding.itemInputAcronyms.setContent(mHeaderBean.memberIntro);
        mBinding.cfkRemark.setEditContent(mHeaderBean.remark);
    }

    @Override
    public void onMultiClick(View v) {
        if (v == mBinding.btnRelease) {
            String content = mBinding.itemInputAcronyms.getContent();
//            if (StringUtil.isEmpty(content)) {
//                MToast.showToast(this, "请填写企业简称");
//                return;
//            }
            String contacts = mBinding.etContacts.getContent();
            if (StringUtil.isEmpty(contacts)) {
                MToast.showToast(this, "请输入联系人");
                return;
            }
            String phone = mBinding.etPhone.getContent();
            if (StringUtil.isEmpty(phone)) {
                MToast.showToast(this, "请输入联系电话");
                return;
            }
            String remark = mBinding.cfkRemark.getEditContent();
            Map<String, Object> map = new HashMap<>();
            map.put("memberIntroduction", content);//会员简称
            map.put("memberType", mMemberBean.getMemberUpdateLeverType()); //会员类型1 供方会员 2 需方会员
            map.put("contact", contacts);
            map.put("phone", phone);
            map.put("remark", remark);
            map.put("firstCategory", getNos(mHeaderBean.firstCategoryNo));
            map.put("secondCategory", getNos(mHeaderBean.secondCategoryNo));
            map.put("thirdCategory", getNos(mHeaderBean.thirdCategoryNo));
            map.put("fourCategory", getNos(mHeaderBean.fourCategoryNo));
            map.put("fiveCategory", getNos(mHeaderBean.fiveCategoryNo));
            map.put("no", mMemberBean.getNo());
            sModel.editCompanyInfo(map);
        }
    }

    private List<String> getNos(List<String> list) {
        if (list == null || list.size() == 0) {
            return new ArrayList<>();
        }
        return list;
    }

    @Override
    public void OnWholeItemClick(View view) {
        if (view.getId() == R.id.item_input_type) {
            String memberUpdateLeverType = mMemberBean.getMemberUpdateLeverType(); //1供方会员 2需方会员
            if (TextUtils.equals("1", memberUpdateLeverType)) {
                // ascription 0 供方 1需方
                ChooseMemberTypeActivity.start(this, entityList, "0", 3210);
            } else {
                ChooseMemberTypeActivity.start(this, entityList, "1", 3210);
            }
        }
    }

    private List<CommonPopEntity> entityList;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 3210) {
            entityList = (List<CommonPopEntity>) data.getSerializableExtra("MEMBER_DATA");
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < entityList.size(); i++) {
                sb.append(entityList.get(i).getTypeName()).append("、");
            }
            String toString = sb.toString();
            String substring = toString.substring(0, toString.length() - 1);
            mBinding.itemInputType.getContent_edt().setText(substring);
        }
    }

    public void editCompanyInfoCallback(Map<String, Object> map) {
        MToast.showToast(this, "修改成功");
        String content = mBinding.itemInputAcronyms.getContent();
        String contacts = mBinding.etContacts.getContent();
        String phone = mBinding.etPhone.getContent();
        String remark = mBinding.cfkRemark.getEditContent();
        MemberBean bean = new MemberBean();
        bean.setCompanyName(content);
        bean.setContacts(contacts);
        bean.setPhone(phone);
        bean.setAddress(remark);
        Intent intent = new Intent();
        intent.putExtra("memberBean", bean);
        setResult(RESULT_OK, intent);
        finish();
    }
}
